![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
jsdom-global
Advanced tools
The jsdom-global npm package is used to set up a global DOM environment in Node.js, which is particularly useful for testing and running scripts that are designed to work in a browser environment. It leverages the jsdom library to create a simulated DOM, allowing developers to run browser-specific code in a Node.js environment.
Global DOM Setup
This feature sets up a global DOM environment by requiring the jsdom-global package. The code sample demonstrates how to initialize the global DOM and log the document object to the console.
require('jsdom-global')();
console.log(window.document);
Simulating Browser APIs
This feature allows you to simulate browser-specific APIs like `alert`. The code sample shows how to override the `alert` function to log messages to the console instead.
require('jsdom-global')();
window.alert = (msg) => console.log(`Alert: ${msg}`);
window.alert('Hello, world!');
Testing with Mocha
This feature is useful for setting up a DOM environment for testing purposes. The code sample demonstrates how to use jsdom-global with the Mocha testing framework and Chai assertion library to test DOM manipulation.
const jsdomGlobal = require('jsdom-global');
const { expect } = require('chai');
jsdomGlobal();
describe('DOM Manipulation', () => {
it('should create a new div element', () => {
const div = document.createElement('div');
document.body.appendChild(div);
expect(document.body.contains(div)).to.be.true;
});
});
The jsdom package is the underlying library that jsdom-global uses to create a simulated DOM environment. While jsdom-global sets up a global environment automatically, jsdom requires more manual setup but offers more control and customization options.
Happy DOM is another package that provides a DOM-like environment for Node.js. It is designed to be lightweight and fast, making it a good alternative to jsdom-global for performance-critical applications.
LinkeDOM is a minimalistic and fast DOM implementation for Node.js. It aims to be a lightweight alternative to jsdom, providing essential DOM functionalities with a focus on performance.
Enables DOM in Node.js
jsdom-global will inject document
, window
and other DOM API into your Node.js environment. Useful for running, in Node.js, tests that are made for browsers.
Requires jsdom.
npm install --save-dev --save-exact jsdom jsdom-global
jsdom-global now requires jsdom v10 or above. If you need jsdom v9 and below, use the previous version (jsdom-global@2
).
Just invoke it to turn your Node.js environment into a DOM environment.
require('jsdom-global')()
// you can now use the DOM
document.body.innerHTML = 'hello'
You may also pass parameters to jsdomGlobal() like so: require('jsdom-global')(html, options)
.
Check the jsdom.jsdom() documentation for valid values for the options
parameter.
To clean up after itself, just invoke the function it returns.
var cleanup = require('jsdom-global')()
// do things
cleanup()
In tape, run it before your other tests.
require('jsdom-global')()
test('your tests', (t) => {
/* and so on... */
})
Simple: Use Mocha's --require
option. Add this to the test/mocha.opts
file (create it if it doesn't exist)
-r jsdom-global/register
Advanced: For finer control, you can instead add it via mocha's before
and after
hooks.
before(function () {
this.jsdom = require('jsdom-global')()
})
after(function () {
this.jsdom()
})
If you prefer to use import
rather than require
, you might want to use jsdom-global/register
instead. Place it on top of your other import calls.
import 'jsdom-global/register'
import React from 'react'
import jQuery from 'jquery'
// ...
If you use Browserify on your tests (eg: smokestack, tape-run, budo, hihat, zuul, and so on), doing require('jsdom-global')()
is a noop. In practice, this means you can use jsdom-global even if your tests are powered by browserify, and your test will now work in both the browser and Node.
Writing your tests (test.js
):
require('jsdom-global')()
// ...do your tests here
Running it with smokestack:
browserify test.js | smokestack # run in a browser
node test.js # or the console
browserify test.js --no-bundle-external # also works (but why bother?)
Running it with Babel (babelify or babel-cli):
browserify test.js -t babelify | smokestack # run in a browser (with babel)
babel-node test.js # or the console
jsdom-global © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).
ricostacruz.com · GitHub @rstacruz · Twitter @rstacruz
[v3.0.2]
May 8, 2017
Image
. ([@jtag05])FAQs
Enable DOM in Node.js
The npm package jsdom-global receives a total of 409,580 weekly downloads. As such, jsdom-global popularity was classified as popular.
We found that jsdom-global demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.